﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 车辆管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CarinfoController : ControllerBase
    {
        IFreeSql db;
        public CarinfoController(IFreeSql _db)
        {
            db = _db;
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">条数</param>
        /// <param name="carnumber">车牌号</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfo(int page, int limit, string? carnumber)
        {
            var list = db.Select<Models.Carinfo, Models.Ownerinfo>()
                .LeftJoin((a, b) => a.Oid == b.Oid)
                .Where((a, b) => ((string.IsNullOrEmpty(carnumber)) || (a.Carnumber.Contains(carnumber)) && (a.Isdelete == false)))
                .Count(out var totoal)
                .Page(page, limit)
                .ToList((a, b) => new
                {
                    a.Cid,
                    a.Cimg,
                    a.Carcolor,
                    a.Carnumber,
                    a.Carremark,
                    a.Createtime,
                    b.Oid,
                    b.Orealname
                });
            var json = new
            {
                code = 200,
                data = list,
                total = totoal
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加车辆信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromBody] Models.Carinfo info)
        {
            try
            {
                info.Createtime = DateTime.Now;
                info.Isdelete = false;
                int list = db.Insert(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(ex);
            }
        }

        /// <summary>
        /// 修改车辆信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update([FromBody] Models.Carinfo info)
        {
            try
            {
                int list = db.Update<Models.Carinfo>().SetSource(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除车辆信息
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int? cid)
        {
            try
            {
                int list = db.Update<Models.Carinfo>(cid)
                    .Set(x => x.Isdelete == true)
                    .ExecuteAffrows();
                return Ok(list > 0);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }
    }
}
